export default {
	state:{
		//存放全局事件
		events:[],
		recorderManager:null,
		//录音时长
		recordTime:0,
		//定时器
		recordTimer:null,
		sendVoice:null
	},
	mutations:{
		//初始化录音管理器
		initRecorderManager(state){
			state.recorderManager = uni.getRecorderManager();
			//监听录音结束
			state.recorderManager.onStop((e)=>{
				if(state.recordTimer){
					clearInterval(state.recordTimer)
					state.recordTimer=null
				}
				//执行发送
				if(typeof state.sendVoice === 'function'){
					state.sendVoice(e.tempFilePath)
				}
				
			})
			
			//监听录音开始
			state.recorderManager.onStart((e)=>{
				state.recordTime=0
				state.recordTimer=setInterval(()=>{
					state.recordTime++
				},1000)
			})
		},
		//注册发送音频事件
		regSendVoiceEvent(state,event){
			state.sendVoice = event
		},
		//注册全局事件
		regEvent(state,event){
			state.events.push(event)
		},
		
		//执行全局事件
		doEvent(state,params){
			
			state.events.forEach(e=>{
				e(params)
			})
		},
		//注销事件
		removeEvent(state,event){
			let index  = state.events.findIndex(item=>{
				return item === event
			})
			
			if(index!=-1){
				state.events.splice(index,1)
			}
		}
	},
	actions:{
		//分发注册全局事件
		aduioOn({commit},event){
			commit('regEvent',event)
		},
		//分发执行全局事件
		aduioEmit({commit},params){
			commit('doEvent',params)
		},
		//分发注销全局事件
		aduioOff({commit},event){
			commit('removeEvent',event)
		},
		//执行发送音频事件
		regSendVoiceEvent(state,params){
			
		},
		
	}
}